package com.gxuwz.medical.domain.policy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;

/**
 * 慢性病政策设置
 * 
 * @author 演示
 * 
 */
public class Policy {
	/**
	 * 年份
	 */
	private String runyear;
	/**
	 * 封顶线
	 */
	private double maxline;
	/**
	 * 状态
	 */
	private String status;
	/**
	 * 备注
	 */
	private String remark;

	public Policy() {

	}

	public Policy(String runyear) throws Exception {
		if (runyear == null) {
			throw new IllegalArgumentException("参数不能为null");
		}
		this.runyear = runyear;
		// 加载数据
		load();
	}

	/**
	 * 设置启用状态
	 * 
	 * @param status
	 * @throws SQLException
	 */
	public void setup(String status) throws SQLException {
		Connection conn = null;
		this.status = status;
		try {
			// 1:对象属性赋值,无
			conn = DbUtil.getConn();
			// 2；调用方法更新状态
			updateStatus(conn);
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	private void load() throws SQLException {
		Connection conn = DbUtil.getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			String sql = "select * from t_policy where runyear=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, this.runyear);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.runyear = rs.getString("runyear");
				this.maxline = rs.getDouble("maxline");
				this.status = rs.getString("status");
				this.remark = rs.getString("remark");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}

	}

	private void updateStatus(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			// 定义构造插入SQL语句字符串变量sql
			String sql = "update t_policy set status=? where runyear=?";
			// 创建执行带动态参数的SQL对象
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, this.status);
			pstmt.setString(index++, this.runyear);
			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);

		}

	}

	public Policy(String runyear, double maxline, String status, String remark) {
		super();
		this.runyear = runyear;
		this.maxline = maxline;
		this.status = status;
		this.remark = remark;
	}

	/**
	 * 添加封顶线
	 */
	public void add() throws SQLException {
		Connection conn = null;
		try {
			// 1:对象属性赋值,无
			conn = DbUtil.getConn();
			// 2；调用saveToDB方法保存到数据库
			saveToDB(conn);
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	private void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			// 定义构造插入SQL语句字符串变量sql
			String sql = "insert into t_policy(runyear,maxline,status,remark)values(?,?,?,?)";
			// 创建执行带动态参数的SQL的PreparedStatement
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, this.runyear);
			pstmt.setDouble(index++, this.maxline);
			pstmt.setString(index++, this.status);
			pstmt.setString(index++, this.remark);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);

		}

	}

	public void delPolicy(String runyear) throws SQLException {
		Connection conn = null;
		try {
			this.runyear = runyear;
			conn = DbUtil.getConn();
			// 开启手动提交事务
			conn.setAutoCommit(false);
			// 保存到数据库
			delFromDB(conn);
			// 提交事务
			conn.commit();
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
			}
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void delFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"delete from t_policy where runyear=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.runyear);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}

	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRunyear() {
		return runyear;
	}

	public void setRunyear(String runyear) {
		this.runyear = runyear;
	}

	public double getMaxline() {
		return maxline;
	}

	public void setMaxline(double maxline) {
		this.maxline = maxline;
	}

	public String getRemark() {
		return remark;
	}

}
